package leetcode.leetcode_234;


public class ListNode {
    int val;
    ListNode next;
    ListNode() {}
    ListNode(int val) { this.val = val; }
    ListNode(int val, ListNode next) { this.val = val; this.next = next; }
}

class Solution {
    public boolean isPalindrome(ListNode head) {
        ListNode midNode = getMidNode(head);
        ListNode rightNode = reverse(midNode);
        while(rightNode!=null){
            if (rightNode.val!=head.val){
                return false;
            }
            rightNode = rightNode.next;
            head = head.next;
        }
        return true;

    }
    public ListNode getMidNode(ListNode node){
        ListNode slow = node;
        ListNode fast = node;
        while(fast.next!=null&&fast.next.next!=null){
            slow = slow.next;
            fast = fast.next.next;
        }
        return slow;
    }
    public ListNode reverse(ListNode node){
        ListNode prev = null;
        ListNode curr = node;
        while(curr!=null){
            ListNode next  = curr.next;
            curr.next = prev;
            prev = curr;
            curr = next;
        }
        return prev;
    }
}